package csu.rjxyg.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import csu.rjxyg.action.constant.PermissionConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.factory.core.BeanFactory;


public class BaseAction extends ActionSupport{
	
	protected static final String SUCCESS = "success";
	protected static final String FAIL			=	"error";
	
	protected static final String USER_SERVICE = "userService";
	protected static final String COURSE_SERVICE = "courseService";
	protected static final String INFO_SERVICE = "infoService";
	
	protected BeanFactory factory = BeanFactory.getBeanFactory();
	
	protected Map getPermissionMap() {
		return PermissionConstant.getPermissionMap();
	}

	protected Map getPermissionNameMap() {
		return PermissionConstant.getPermissionNameMap();
	}
	
	protected Map getSession() {
		return ActionContext.getContext().getSession();
	}
	
	protected ActionContext getContext() {
		return ActionContext.getContext();
	}
	
	protected void out(String string) throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse(); 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter(); 
		out.print(string);
		
	    out.flush();  
	    out.close(); 
	}
	
	protected void out(String[] string) throws IOException{
		HttpServletResponse response=ServletActionContext.getResponse(); 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter(); 
		out.print(string);
		
	    out.flush();  
	    out.close(); 
	}
	
	protected BaseList changePermissionName(BaseList baseBean){
		BaseList result = (BaseList) this.factory.getBean(BaseBean.BEAN_LIST);
		for( int i = 0;i < baseBean.size();i++ ){
			BaseObject bean = baseBean.get(i);
			List oldList = (List) bean.get(UserConstant.USER_QX);
			List newList = new ArrayList();
			for( int j = 0; j < oldList.size();j++ ){
				newList.add(this.getPermissionNameMap().get(oldList.get(j)));
			}
			bean.set(UserConstant.USER_QX, newList);
			result.add(bean);
		}
		
		return result;
	}
	
	
	protected BaseObject changePermissionName(BaseObject baseBean){
			List oldList = (List) baseBean.get(UserConstant.USER_QX);
			List newList = new ArrayList();
			for( int j = 0; j < oldList.size();j++ ){
				newList.add( this.getPermissionNameMap().get(oldList.get(j)));
			}
			baseBean.set(UserConstant.USER_QX, newList);
		
		return baseBean;
	}
	
	
	protected BaseList checkPermission(BaseList baseBean, String permission){
		int per = Integer.parseInt(permission);
		int[] temp = this.getPermissionUpAndDown(per);
		int up = temp[0];
		int down = temp[1];
		BaseList result = (BaseList) this.factory.getBean(BaseBean.BEAN_LIST);
		for( int i = 0;i < baseBean.size();i++ ){
			//获取每一个用户的信息
			BaseObject bean = baseBean.get(i);
			
			//获取每一个用户的所有权限
			List oldList = (List) bean.get(UserConstant.USER_QX);
			List newList = new ArrayList();
			//标记该用户是否在指定权限段之内
			boolean flag = false;
			
			for( int j = 0; j < oldList.size();j++ ){
				//获取该用户的权限
				int temp1 = Integer.parseInt((String) oldList.get(j));
				
				
				if( temp1 >= up &&  temp1 <= down ){
					flag = true;
					newList.add(oldList.get(j));
				}
			}
			if( flag == true ){
				bean.set(UserConstant.USER_QX, newList);
				result.add(bean);
			}
			
			
		}
		
		return result;
	}
	
	protected int[] getPermissionUpAndDown(int per){
		int[] temp = new int[2];
		//权限段上下限
		int up;
		int down;
		
		switch(per){
			case 0: up = 0;
							down = 1000;
							break;
			case 1:	up = 0;
							down = 300;
							break;
			case 101: up = 100;
								down = 200;
								break;
			case 111: up = 110;
								down = 120;
								break;
			case 112: up = 110;
								down = 120;
								break;
			case 121: up = 120;
								down = 130;
								break;
			case 122: up = 120;
								down = 130;
								break;
			case 131: up = 130;
								down = 140;
								break;
			case 132: up = 130;
								down = 140;
								break;
			case 201: up	= 200;
								down = 400;
								break;
			case 202: up	= 200;
								down = 400;
								break;
			case 203: up = 200;
								down = 400;
								break;
			default: return null;
		}
		temp[0] = up;
		temp[1] = down;
		return temp;
	}
}
